-
Notifications
You must be signed in to change notification settings - Fork 138
feat: ClientContext and secure parameters support #4316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Summary of ChangesHello @aseering, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Java Spanner client library by adding robust support for Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces support for ClientContext in the Spanner Java client, enabling the use of secure parameters. The changes add a clientContext option at both the global SpannerOptions level and the per-request Options level, with logic to merge them. My review identifies a potential issue where the default ClientContext is not being applied to CommitRequests, which is inconsistent with other request types. I've also included a couple of suggestions to refactor duplicated and verbose code for better maintainability.
I am having trouble creating individual review comments. Click here to see my feedback.
google-cloud-spanner/src/main/java/com/google/cloud/spanner/TransactionRunnerImpl.java (467-470)
The RequestOptions for the CommitRequest are built from per-transaction options but do not appear to merge with the default ClientContext that can be configured on SpannerOptions. This is inconsistent with how BeginTransactionRequest and ExecuteSqlRequest are handled in this PR. This could lead to the ClientContext being unexpectedly missing from commit requests when a default is expected to be applied.
Consider applying the same merge logic used in SessionImpl.beginTransactionAsync here to ensure consistent behavior.
RequestOptions requestOptions = options.toRequestOptionsProto(true);
RequestOptions.ClientContext defaultClientContext =
session.getSpanner().getOptions().getClientContext();
if (defaultClientContext != null) {
RequestOptions.ClientContext.Builder clientContextBuilder =
defaultClientContext.toBuilder();
if (requestOptions.hasClientContext()) {
clientContextBuilder.mergeFrom(requestOptions.getClientContext());
}
requestOptions =
requestOptions.toBuilder().setClientContext(clientContextBuilder.build()).build();
}
if (!requestOptions.equals(RequestOptions.getDefaultInstance())) {
requestBuilder.setRequestOptions(requestOptions);
}google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionImpl.java (488-497)
The logic for merging the default ClientContext from SpannerOptions with the request-specific ClientContext is also present in AbstractReadContext.buildRequestOptions. To improve maintainability and reduce code duplication, consider extracting this logic into a shared helper method.
google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ConnectionImpl.java (2091-2098)
The logic for appending the clientContext as an UpdateOption is a bit verbose. For consistency with mergeQueryRequestOptions, which uses an appendQueryOption helper, consider creating and using a similar helper method like appendUpdateOption.
if (clientContext != null) {
options = appendUpdateOption(options, Options.clientContext(clientContext));
}6081b9e to
dccce56
Compare
This change adds support for ClientContext in Options and ensures it is propagated to ExecuteSql, Read, Commit, and BeginTransaction requests. It aligns with go/spanner-client-scoped-session-state design. - Added RequestOptions.ClientContext to Options. - Refactored request option building to Options.toRequestOptionsProto. - Updated AbstractReadContext, TransactionRunnerImpl, and SessionImpl to use the shared logic. - Added tests.
4652157 to
160e1db
Compare
This change adds support for setting and propagating ClientContext in the Spanner Connection API. ClientContext allows propagating client-scoped session state (e.g., secure parameters) to Spanner RPCs. - Added setClientContext/getClientContext to Connection interface and implementation. - Implemented state propagation from Connection to UnitOfWork and its implementations (ReadWriteTransaction, SingleUseTransaction). - Fixed accidental import removal in OptionsTest.java. - Fixed TransactionRunnerImplTest to correctly verify ClientContext propagation. - Added ClientContextMockServerTest for end-to-end verification.
160e1db to
4556e81
Compare
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds support for secure parameters (ClientContext) to the Spanner Java client. The changes are comprehensive, touching various layers from SpannerOptions down to the connection implementation. The logic for merging default and request-specific client contexts is correctly implemented. The addition of tests, including a mock server test, ensures the new functionality is well-covered. I've included a couple of suggestions to improve the efficiency of building RequestOptions by reducing object creation, and another to improve maintainability by reducing code duplication. Overall, this is a solid feature addition.
google-cloud-spanner/src/main/java/com/google/cloud/spanner/AbstractReadContext.java
Show resolved
Hide resolved
google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionImpl.java
Outdated
Show resolved
Hide resolved
| if (clientContext != null) { | ||
| if (options == null || options.length == 0) { | ||
| options = new UpdateOption[] {Options.clientContext(clientContext)}; | ||
| } else { | ||
| options = Arrays.copyOf(options, options.length + 1); | ||
| options[options.length - 1] = Options.clientContext(clientContext); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block for appending clientContext is a bit verbose and duplicates the logic for appending rpcPriority above. This pattern could be extracted into a helper method, similar to appendQueryOption which already exists in this class, to improve readability and reduce code duplication.
For example, you could introduce an appendUpdateOption method:
private UpdateOption[] appendUpdateOption(UpdateOption[] options, UpdateOption append) {
if (options == null || options.length == 0) {
return new UpdateOption[] {append};
}
UpdateOption[] newOptions = Arrays.copyOf(options, options.length + 1);
newOptions[newOptions.length - 1] = append;
return newOptions;
}Then mergeUpdateRequestOptions could be simplified to:
private UpdateOption[] mergeUpdateRequestOptions(UpdateOption... options) {
if (getConnectionPropertyValue(RPC_PRIORITY) != null) {
options = appendUpdateOption(options, Options.priority(getConnectionPropertyValue(RPC_PRIORITY)));
}
if (clientContext != null) {
options = appendUpdateOption(options, Options.clientContext(clientContext));
}
return options;
}This would make the mergeUpdateRequestOptions method more concise and maintainable.
olavloite
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks largely good to me with a few nitpicks
Also check whether the last suggestions from Gemini make sense.
google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java
Outdated
Show resolved
Hide resolved
...d-spanner/src/test/java/com/google/cloud/spanner/connection/ClientContextMockServerTest.java
Outdated
Show resolved
Hide resolved
...d-spanner/src/test/java/com/google/cloud/spanner/connection/ClientContextMockServerTest.java
Show resolved
Hide resolved
google-cloud-spanner/src/test/java/com/google/cloud/spanner/OptionsTest.java
Show resolved
Hide resolved
google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionImplTest.java
Outdated
Show resolved
Hide resolved
google-cloud-spanner/src/test/java/com/google/cloud/spanner/TransactionRunnerImplTest.java
Outdated
Show resolved
Hide resolved
4fc2e27 to
c4fd3ae
Compare
Add support for
ClientContextand secure parameters.